﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace CSMF
{
    public partial class CSMF : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var webWorkContext = new WebWorkContext();
            var user = webWorkContext.CurrentUser;

            this.logoutBtn.Visible = false;
            //this.loginBtn.Visible = false;
            //this.gotoreg.Visible = false;

            if (user != null && user.IsRegistered())
            {
                userName.Text = "您好， " + user.UserName;
                this.logoutBtn.Visible = true;
                ShowLoginInfo(false);
            }
            else
            {
                //this.loginBtn.Visible = true;
                //this.gotoreg.Visible = true;
                ShowLoginInfo(true);
            }

            SetSelected();
        }

        private void SetSelected()
        {
            string url = Request.Url.AbsolutePath.ToLower();
            if (url.Contains(LinkButton1.PostBackUrl.ToLower()))
            {
                LinkButton1.CssClass = "n1 selected";
            }
            if (url.Contains(LinkButton2.PostBackUrl.ToLower()) || url.Contains("aboutmatch"))
            {
                LinkButton2.CssClass = "n2 selected";
            }
            if (url.Contains(LinkButton3.PostBackUrl.ToLower()))
            {
                LinkButton3.CssClass = "n3 selected";
            }
            if (url.Contains(LinkButton4.PostBackUrl.ToLower()) || url.Contains("merakdetail"))
            {
                LinkButton4.CssClass = "n4 selected";
            }
            if (url.Contains(LinkButton5.PostBackUrl.ToLower()) || url.Contains("mayordetail"))
            {
                LinkButton5.CssClass = "n5 selected";
            }
            if (url.Contains(LinkButton6.PostBackUrl.ToLower()) || url.Contains("themedetail"))
            {
                LinkButton6.CssClass = "n6 selected";
            }
            if (url.Contains(LinkButton7.PostBackUrl.ToLower()) || url.Contains("newsdetail"))
            {
                LinkButton7.CssClass = "n7 selected";
            }
            if (url.Contains(LinkButton8.PostBackUrl.ToLower()))
            {
                LinkButton8.CssClass = "n8 selected";
            }
            if (url.Contains(LinkButton9.PostBackUrl.ToLower()))
            {
                LinkButton9.CssClass = "n9 selected";
            }
            if (url.Contains(LinkButton10.PostBackUrl.ToLower()) || url.Contains("j0"))
            {
                LinkButton10.CssClass = "n10 selected";
            }
        }

        private void ShowLoginInfo(bool isVisible)
        {
            lblPasswordHint.Visible = isVisible;
            lblUserNameHint.Visible = isVisible;

            Password.Visible = isVisible;
            gogo.Visible = isVisible;
            LoginName.Visible = isVisible;
            logoutBtn.Visible = !isVisible;
            sinaLoginLabel.Visible = isVisible;
        }

        protected void LogoutBtn_Click(object sender, EventArgs e)
        {
            var authenticationService = new AuthenticationService();
            authenticationService.SignOut();
            Response.Redirect(Request.RawUrl);

        }

        protected void GotoReg_Click(object sender, EventArgs e)
        {
            Response.Redirect("regist.aspx");
        }

        protected void LoginBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }

        protected void SinaLoginBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("SinaLogin.aspx");
        }

        protected void GoLogon_Click(object sender, EventArgs e)
        {
            var authenticationService = new AuthenticationService();
            var userRegistrationSerivce = new UserRegistrationService();
            var userService = new UserService();

            if (userRegistrationSerivce.ValidateCustomer(this.LoginName.Text, this.Password.Text))
            {
                var user = userService.GetCustomerByUsername(this.LoginName.Text);

                //sign in new customer
                authenticationService.SignIn(user, true);
                Response.Redirect(Request.RawUrl);
            }
            
        }
    }
}